package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;


    @RequiredPermission(name="角色页面",expression="role:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        Page<Role> pageInfo = roleService.queryPage(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "role/list";
    }


    @RequiredPermission(name="角色删除",expression="role:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           roleService.removeById(id);
        }
        return "redirect:/role/list";
    }


    @RequiredPermission(name="角色新增/编辑",expression="role:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role) {
        if (role.getId() == null) {
            roleService.save(role);
        } else {
            roleService.updateById(role);
        }
        return "redirect:/role/list";
    }


    @RequiredPermission(name="角色新增/编辑",expression="role:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("role",roleService.getById(id));
        }
        return "role/input";
    }
}
